<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/1/7
 * Time: 18:25
 */

namespace common\sources\write\regqrcode;

use common\models\craftsman\RegQrCode;
use Yii;

class RegQrCodeAccess implements IRegQrCodeAccess
{
    public function setRegQrCodeImg($data)
    {
        $regQrCode = RegQrCode::findOne(['id' => $data['qrcode_id']]);
        if (!$regQrCode) {//没有这个二维码
            return false;
        }
        $regQrCode->qrcode_img = $data['qrcode_img'];
        $regQrCode->update_time = date("Y-m-d H:i:s",time());
        if ($regQrCode->save()) {
            return true;
        } else {
            return false;
        }
        // TODO: Implement setRegQrCodeImg() method.
    }

    public function UseQrCode($data)
    {
        $sql = 'UPDATE ' . RegQrCode::tableName() . ' SET is_use = :is_refus';//先将所有状态更为禁用
        $sql_1 = 'UPDATE ' . RegQrCode::tableName() . ' SET is_use = :is_use WHERE id = :id';//设置启用的二维码
        $result = Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':is_refus' => 2,
            ])
            ->execute();
        $result_1 = Yii::$app->db->createCommand($sql_1)
            ->bindValues([
                ':is_use' => $data['is_use'],
                ':id' => $data['qrcode_id'],
            ])
            ->execute();
        return $result_1;
        // TODO: Implement UseQrCode() method.
    }

    public function DelQrCode($data)
    {
        $result = RegQrCode::findOne($data['qrcode_id'])
            ->delete();
        return $result;
        // TODO: Implement DelQrCode() method.
    }

    public function InsQrCode($data)
    {
        $now_qrcode = new RegQrCode();
        $now_qrcode->qrcode_name = $data['qrcode_name'];
        $now_qrcode->qrcode_img = $data['qrcode_img'];
        if (!$now_qrcode->save()) {
            return false;
        }
        return true;
        // TODO: Implement InsQrCode() method.
    }
}